import sys

def write_groups(points_filename, index_points_map):
	keys = index_points_map.keys()

	for key in keys:

		fptr = open(points_filename + "." + key, "w")
		points_of_group = index_points_map[key]

		for line in points_of_group:
			fptr.write(line + "\n")

		fptr.close() 

def split_files(points_filename, index_filename):
	index_points_map = dict()

	points_fptr = open(points_filename, "r")
	index_fptr = open(index_filename, "r")

	line = points_fptr.readline().rstrip()
	index = index_fptr.readline().rstrip()

	while line != "":

		if (index_points_map.has_key(index) == False):
			index_points_map[index] = [line]
		else:
			index_points_map[index].append(line)

		line = points_fptr.readline().rstrip()
		index = index_fptr.readline().rstrip()
	
	points_fptr.close()
	index_fptr.close()

	write_groups(points_filename, index_points_map)

if __name__ == "__main__":
	argv = sys.argv
	argc = len(argv)

	if (argc < 3):
		print ""
		print "Use python", argv[0], "<points> <index>"
		print ""

	else:
		points_filename = argv[1]
		index_filename = argv[2]

		split_files(points_filename, index_filename)
